package com.iximo.openAPI.impl;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import com.iximo.domain.Chapter;
import com.iximo.domain.Member;
import com.iximo.domain.Results;
import com.iximo.openAPI.IximoOpenAPIPost;
import com.iximo.util.BaseUrl;
import com.iximo.util.InternetService;
import com.thoughtworks.xstream.XStream;


public class IximoOpenAPIPostImpl implements IximoOpenAPIPost {

	public Member login(int clientType, String email, String password) throws Exception {
		String uri = BaseUrl.loginUrl + "?client_type=" + clientType;
		Map params = new HashMap();
		params.put("email", email);
		params.put("password", password);
		InputStream inStream = InternetService.post(uri, params);
		InputStreamReader readInput = new InputStreamReader(inStream, "utf-8");
		XStream xStream = new XStream();
		xStream.alias("member", Member.class);
		Member member = (Member)xStream.fromXML(readInput);
		return member;
	}
	
	public Member loginData(InputStream inStream) throws Exception {
		InputStreamReader readInput = new InputStreamReader(inStream, "utf-8");
		XStream xStream = new XStream();
		xStream.alias("member", Member.class);
		Member member = (Member)xStream.fromXML(readInput);
		return member;
	}

	public int sendComment(int bookId, String comment, int commentId) throws Exception {
		String uri = BaseUrl.addCommentUrl;	
		Map params = new HashMap();
		params.put("comment", comment);
		params.put("book_id", String.valueOf(bookId));
		params.put("comment_id", String.valueOf(commentId));
		InputStream inStream = InternetService.post(uri, params);
		String result = InternetService.readStream(inStream);
		return Integer.valueOf(result);
	}

	public Member register(int clientType, String email, String password) throws Exception {
		String uri = BaseUrl.registerUrl + "?client_type=" + clientType;
		Map params = new HashMap();
		params.put("email", email);
		params.put("password", password);
		InputStream inStream = InternetService.post(uri, params);
		InputStreamReader readInput = new InputStreamReader(inStream, "utf-8");
		XStream xStream = new XStream();
		xStream.alias("member", Member.class);
		Member member = (Member)xStream.fromXML(readInput);
		return member;
	}
	
	public Member registerData(InputStream inStream) throws Exception {
		InputStreamReader readInput = new InputStreamReader(inStream, "utf-8");
		XStream xStream = new XStream();
		xStream.alias("member", Member.class);
		Member member = (Member)xStream.fromXML(readInput);
		return member;
	}

	public int modifyProfile(String username, int gender, String qq,
			String msn, String phone) throws Exception {
		String uri = BaseUrl.editProfileUrl;
		Map params = new HashMap();
		params.put("username", username);
		params.put("gender", String.valueOf(gender));
		params.put("qq", qq);
		params.put("msn", msn);
		params.put("phone", phone);
		InputStream inStream = InternetService.post(uri, params);
		String ret = InternetService.readStream(inStream);
		return Integer.valueOf(ret);
	}

}
